/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "FontManager.h"
#include "../kernel.h"

extern Kernel* g_kernel;


FontManager::FontManager(void)
{
    m_colors[0].SetData( 0, 0, 0, 1);                   // black
    m_colors[1].SetData( 1, 1, 1, 1);                   // white
    m_colors[2].SetData( 0.23f, 0.23f, 0.23f, 1);       // grey
    m_colors[3].SetData( 1, 0, 0, 1);                   // red
    m_colors[4].SetData( 0, 1, 0, 1);                   // green
    m_colors[5].SetData( 0, 0, 1, 1);                   // blue
    m_colors[6].SetData( 0.89f, 0.52f, 0.12f, 1);       // orange
    m_colors[7].SetData( 0.65f, 0.03f, 0.65f, 1);       // purple
    m_colors[8].SetData( 0.23f, 0.89f, 0.89f, 1);       // light blue
    m_colors[9].SetData( 0.52f, 1, 0.52f, 1);           // light green

    m_currentFont = NULL;
}

FontManager::~FontManager(void)
{
    FreeAll();
}

/** Free all */
void FontManager::FreeAll()
{
    type_FontList::iterator it = m_fonts.begin();
    for( ; it != m_fonts.end(); ++it )
    {
        (*it).second->Destroy();
        delete (*it).second;
    }
    m_fonts.clear();
}

/** Create a font */
bool FontManager::Create( const std::string &file, const std::string &fontName )
{
    FontType* font = new FontType;

    bool success = font->Create( file, 1 );
    font->height = 12;       //  defaults
    font->width  = 8.0f;
    font->size   = 12;
    font->name   = fontName;

    if ( success )
        m_fonts[ fontName ] = font;

    return success;
}

/** Set Font Type */
void FontManager::SetFontType( const std::string &fontName )
{
    if ( m_fonts.find( fontName ) != m_fonts.end() )
        m_currentFont = m_fonts[ fontName ];
    
}

/** Free the font */
void FontManager::Free(const std::string &fontName)
{
    type_FontList::iterator it = m_fonts.find( fontName );
    if ( it != m_fonts.end() )
    {
        m_fonts[ fontName ]->Destroy();
        delete m_fonts[ fontName ];
        m_fonts.erase( it );
    } 
}

/** Draw a String */
void FontManager::DrawString( IRender* r, float x, float y, const char* s,... )
{
	char		text[1024];								// Holds Our String
	va_list		ap;										// Pointer To List Of Arguments

    if (s   == NULL)									// If There's No Text
		return;											// Do Nothing

	va_start(ap, s);									// Parses The String For Variables
	    vsprintf(text, s, ap);							// And Converts Symbols To Actual Numbers
	va_end(ap);											// Results Are Stored In Text

    int currentColor = -1;
    for ( int i = 0; text[i]; ++i )
    {
        // check for a color changer
        if ( text[i] == '^' )
        {
            if ( text[i+1] )
            {
                char c = text[i+1];
                if ( c >= 48 && c <= 57 ) {
                    currentColor =  c - 48;
                    i += 1; continue;
                }
            }
        }
        DrawChar( r, currentColor, x, y, text[i] );
        x += m_currentFont->width;
    }

}

/** Draw out a single char */
void FontManager::DrawChar( IRender* r, int color, float x, float y, char c )
{
    Color old = r->GetColor();
    if (color > -1) r->SetColor( m_colors[ color ] );           // set the proper color
        m_currentFont->Begin();                 // bind the texture
        m_currentFont->DrawChar( x, y, c );     // finally draw it
    r->SetColor( old );
    
}